package com.example.neusoft.yuejian_12.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.Activity.ClubDetailActivity;
import com.example.neusoft.yuejian_12.Activity.MasterDetailActivity;
import com.example.neusoft.yuejian_12.Activity.UserCollectActivity;
import com.example.neusoft.yuejian_12.JavaBean.UserCollect;
import com.example.neusoft.yuejian_12.Listener.ClubStateListener;
import com.example.neusoft.yuejian_12.Listener.UserCollectListener;
import com.example.neusoft.yuejian_12.Listener.UserNoCollectListener;
import com.example.neusoft.yuejian_12.Model.ClubModel;
import com.example.neusoft.yuejian_12.Model.UserModel;
import com.example.neusoft.yuejian_12.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 晓真 on 2017/10/24.
 */

public class UserCollectListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;//承载上下文
    private List<UserCollect> mDataList;//动态数组承载数据使用 数据存储
    private LayoutInflater mLayoutInflater; //准备布局
    private String userid, clubId,fans_type, coresult;
//    private String user_name,user_photo;

    public UserCollectListAdapter(Context mContext, List<UserCollect> mDataList, String userid){
        this.mContext=mContext;
        this.mDataList=mDataList;
        this.userid = userid;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=mLayoutInflater.from(parent.getContext()).inflate(R.layout.user_collect_item,parent,false);
        return new UserCollectListAdapter.ViewHolder(v);
    }

    //将数据绑定到控件
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final  UserCollect entity= mDataList.get(position);
        if (null==entity)
            return;
        UserCollectListAdapter.ViewHolder viewHolder= (UserCollectListAdapter.ViewHolder) holder;
        viewHolder.collect_name.setText(entity.getName());
        viewHolder.club_id.setText(entity.getFan_id());
       /* viewHolder.type.setText(entity.getFans_type());*/
        clubId = viewHolder.club_id.getText().toString();


       /* fans_type = viewHolder.type.getText().toString();*/
        if(!entity.getThumb().isEmpty()) {
            Picasso.with(mContext).load("http://172.24.10.175/workout/Uploads/"+entity.getThumb()).into(viewHolder.collect_pic);
        } else {
            viewHolder.collect_pic.setImageResource(R.drawable.qya);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(mContext,MasterDetailActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("id",entity.getFan_id());
                intent.putExtra("title",entity.getName());
                mContext.startActivity(intent);
            }
        });

        viewHolder.collect_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel userModel = new UserModel();
                userModel.Nocollect(userid ,clubId,"2",usernocollectListener);
            }
        });
    }

    UserNoCollectListener usernocollectListener=new UserNoCollectListener() {
        @Override
        public void onResponse(Integer stringBean) {
            Toast.makeText(mContext,"取消收藏成功",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(mContext,"取消收藏失败",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    //找到控件，将其初始化
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView collect_pic;
        TextView collect_name,club_id;
        ImageView collect_button;
        public ViewHolder(View itemView) {
            super(itemView);
            collect_name = (TextView) itemView.findViewById(R.id.collect_club_name);
            collect_pic = (ImageView) itemView.findViewById(R.id.collect_club_pic);
            collect_button = (ImageView) itemView.findViewById(R.id.collect_club_button);
            club_id = (TextView) itemView.findViewById(R.id.collect_club_id);
        }
    }
}

